package com.ecarx.module_udisk.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.module_udisk.R;
import com.ecarx.module_udisk.bean.PathBean;

import java.util.ArrayList;
import java.util.List;


public class UdiskPathAdapter extends RecyclerView.Adapter<UdiskPathAdapter.MyViewHolder> {
    private static final String TAG = "UdiskPathAdapter";
    private Context mContext;
    private List<PathBean> pathList;

    private boolean mEnable;

    private UdiskPathAdapter.recycleListStatusCallBack callBack;

    public void setEnable(boolean enable) {
        this.mEnable = enable;
        notifyDataSetChanged();
    }

    public void setCallBack(UdiskPathAdapter.recycleListStatusCallBack callBack) {
        this.callBack = callBack;
    }
    public UdiskPathAdapter(Context context) {
        this.mContext = context;
    }

    public void setPathList(List<PathBean> pathList) {
        this.pathList = pathList;
        notifyDataSetChanged();
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_path, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        PathBean pathBean = pathList.get(position);
        holder.tv.setText(pathBean.getShowPath());
        holder.cb.setOnCheckedChangeListener(null);
        holder.cb.setChecked(pathBean.isSelected());
        holder.cb.setEnabled(mEnable);
        holder.cb.setOnCheckedChangeListener((compoundButton, b) ->{
                pathBean.setSelected(b);
                if(b){
                    pathList.stream().filter(pathBean1 -> !pathBean1.getPath().equals(pathBean.getPath()))
                            .forEach(pathBean1 ->pathBean1.setSelected(false));
                }
                callBack.AnyPathCheckStatus(b);
        });
    }



    @Override
    public int getItemCount() {
        if (pathList == null || pathList.size() == 0) {
            return 0;
        }
        return pathList.size();
    }

    /**
     * 获取选中升级的文件路径
     *
     * @return
     */
    public String getCheckPath() {
        if(pathList == null){
            return null;
        }
        List<String> list = new ArrayList<>();
        for (PathBean bean : pathList) {
            if (bean.isSelected()) {
                return bean.getPath();
            }
        }
        return null;
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        CheckBox cb;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.text);
            cb = itemView.findViewById(R.id.cb_check);
        }

    }

    public interface recycleListStatusCallBack {
        void AnyPathCheckStatus(boolean b);
    }
}
